//
//  CompanyDetailViewController.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/17.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "CompanyDetailViewController.h"
#import "WokerViewController.h"

#import "HBHCompanyPropertyCell.h"
#import "detailheaderFindCell.h"
#import "DetaileJobCell.h"
#import "scrollVCell.h"
#import "HBHWebViewCell.h"
@interface CompanyDetailViewController ()<UITableViewDelegate,UITableViewDataSource,UIWebViewDelegate>

@property (nonatomic,strong)UITableView *tableV;
@property (nonatomic, retain) NSDictionary *data;
@property (nonatomic,assign)BOOL isMain;//是不是主页

@property (nonatomic) CGFloat webH;
@property (nonatomic, strong) UIWebView *web;
@end

@implementation CompanyDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatView];
    [self getDetail];
}

-(void)creatView
{
    _isMain = YES;
    [self initTitleWithTitle:@"公司详情"];
    [self.view addSubview:self.tableV];
}

-(UIWebView *)web
{
    if (!_web) {
        _web = [[UIWebView alloc]init];
        _web.delegate = self;
    }
    return _web;
}

//Web加载完成
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    //计算webView的高
    CGRect frame = webView.frame;
    frame.size.width = SCREENW;
    frame.size.height = 1;
    webView.scrollView.scrollEnabled = NO;
    webView.frame = frame;
    frame.size.height = webView.scrollView.contentSize.height;
    webView.frame = frame;
    _webH = frame.size.height;
    [_tableV reloadData];
}

-(UITableView *)tableV
{
    if (!_tableV) {
        _tableV = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREENW, SCREENH-64) style:UITableViewStyleGrouped];
        _tableV.backgroundColor = backColor;
        _tableV.estimatedRowHeight = 175*screenW_6;
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableV.showsVerticalScrollIndicator = NO;
        _tableV.delegate = self;
        _tableV.dataSource = self;
//        _tableV.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
//            
//        }];
    }
    return _tableV;
}

#pragma Mark TableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        if (_isMain) {//是主页
            return 2;
        }
        return 1;
    }
    if (_isMain) {//是主页
        return 2;
    }
    NSArray *arr = _data[@"positionBeanList"];
    return arr.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if(_isMain){//是主页
        return 2;
    }
    return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.000001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.000001;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {//区头公司
        if (indexPath.row == 0) {
            detailheaderFindCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detailheaderFindCell"];
            if (cell == nil) {
                cell = [[detailheaderFindCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"detailheaderFindCell"];
            }
            [cell setDataDic:_data];
            cell.chooseMainBlock = ^(NSInteger chooseNumber)
            {   //主业 职业 按钮选择
                _isMain = chooseNumber==0?YES:NO;
                [_tableV reloadData];
            };
            return cell;
        }else//主页的时候加载的scrollV
        {
            scrollVCell *cell = [tableView dequeueReusableCellWithIdentifier:@"scrollVCell"];
            if (cell == nil) {
                cell = [[scrollVCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"scrollVCell"];
            }
            NSMutableArray *arr = [NSMutableArray new];
            if (_data[@"companyImg1"]) {
                [arr addObject:_data[@"companyImg1"]];
            }
            if (_data[@"companyImg2"]) {
                [arr addObject:_data[@"companyImg2"]];
            }
            if (_data[@"companyImg3"]) {
                [arr addObject:_data[@"companyImg3"]];
            }
            [cell loadCellWithData:arr];
            return cell;
        }
    }else//职位Cell
    {
        if (_isMain) {//是否是主页
            if (indexPath.row == 0) {
                HBHCompanyPropertyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HBHCompanyPropertyCell"];
                if (!cell) {
                    cell = [[HBHCompanyPropertyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HBHCompanyPropertyCell"];
                }
                [cell loadCellWithData:_data];
                return cell;
            }else{
                HBHWebViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HBHWebViewCell"];
                if (!cell) {
                    cell = [[HBHWebViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HBHWebViewCell"];
                }
                [cell loadCellWithWeb:self.web];
                return cell;
            }
        }else
        {
            DetaileJobCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetaileJobCell"];
            if (cell == nil) {
                cell = [[DetaileJobCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DetaileJobCell"];
            }
            //加载cell
            NSArray *arr = _data[@"positionBeanList"];
            [cell loadCellWithData:arr[indexPath.row]];
            return cell;
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return 130*screenW_6;
        }else{
            return 180*screenW_6;
        }
    }else{
        if (_isMain) {
            if (indexPath.row == 1) {
                return _webH;
            }
            return 120*screenW_6;
        }else{
            return 74*screenW_6;
        }
    }
    return 0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!_isMain && indexPath.section == 1) {
        NSArray *arr = _data[@"positionBeanList"];
        WokerViewController *vc = [WokerViewController new];
        vc.wordId = arr[indexPath.row][@"id"];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - request
-(void)getDetail
{
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/recruit/companyDetails" Dic:@{@"id":_companyId} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
                if ([responseDic[@"success"] boolValue]) {
                    _data = responseDic[@"data"];
                    NSString *content = [NSString stringWithFormat:@"<head><style>img{max-width:%fpx ;height: auto !important}</style></head>%@",SCREENW - 10,_data[@"content"]];
                    [self.web loadHTMLString:content baseURL:nil];
                    [_tableV reloadData];
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
                if ([_tableV.mj_header isRefreshing]) {
                    [_tableV.mj_header endRefreshing];
                }
            });
        }];
    });
}

@end
